package com.lanjiu.lib.business;

import com.google.protobuf.ByteString;
import com.lanjiu.pro.file.BigFileFoot;
import com.lanjiu.pro.file.BigFileFragment;
import com.lanjiu.pro.file.BigFileHead;
import com.lanjiu.pro.file.FileContent;

import java.nio.ByteBuffer;

/**
 * @description: FileContent工厂类
 * @author: Panwei
 * @date: 2019/11/1 20:07
 */
public class FileContentFactory {

    private FileContentFactory() {
    }

    /**
     * 获取下载FileContent
     *
     * @param serverPath 服务器地址
     * @return
     */
    public static FileContent createDownloadFileContent(String serverPath) {
        return FileContent.newBuilder()
                .setBigFileHead(BigFileHead.newBuilder()
                        .setServerUrl(serverPath)
                        .build())
                .setPackageKind(TransmissionConstant.FILE_DOWNLOAD)
                .build();
    }

    /**
     * 获取断点下载FileContent
     *
     * @param cache 缓存
     * @return
     */
    public static FileContent createDownloadFileContent(FileTransmissionCache cache) {
        return FileContent.newBuilder()
                .setBigFileHead(BigFileHead.newBuilder()
                        .setServerUrl(cache.getServerPath())
                        .build())
                .setBigFileFragment(BigFileFragment.newBuilder()
                        .setPosition(cache.getTransmissionPosition())
                        .build())
                .setPackageKind(TransmissionConstant.FILE_FRAGMENT)
                .build();
    }

    /**
     * 根据进度获取FileContent
     *
     * @param position 位置
     * @return
     */
    public static FileContent createDownloadFileContentByPosition(String serverPath, long position) {
        return FileContent.newBuilder()
                .setBigFileHead(BigFileHead.newBuilder()
                        .setServerUrl(serverPath)
                        .build())
                .setBigFileFragment(BigFileFragment.newBuilder()
                        .setPosition(position)
                        .build())
                .setPackageKind(TransmissionConstant.FILE_FRAGMENT)
                .build();
    }

    /**
     * 获取文件传输结果
     *
     * @param isSuccess 是否传输成功
     * @return
     */
    public static FileContent createDownloadFileContentFoot(String serverPath, boolean isSuccess) {
        return FileContent.newBuilder()
                .setBigFileHead(BigFileHead.newBuilder()
                        .setServerUrl(serverPath)
                        .build())
                .setBigFileFoot(BigFileFoot.newBuilder()
                        .setIsSuccess(isSuccess)
                        .build())
                .setPackageKind(TransmissionConstant.FILE_FOOT)
                .build();
    }

    /**
     * 上传文件头
     *
     * @param count      数据包数量
     * @param length     文件长度
     * @param clientPath 文件客户端地址
     * @return
     */
    public static FileContent createUploadFileContentHead(int count, long length, String clientPath) {
        return FileContent.newBuilder().setBigFileHead(
                BigFileHead.newBuilder()
                        .setCount(count)
                        .setLength(length)
                        .setClientUrl(clientPath)
                        .build())
                .setPackageKind(TransmissionConstant.FILE_HEAD)
                .build();
    }

    /**
     * 上传文件包
     *
     * @param buffer   数据包
     * @param size     数据包长度
     * @param position 位置
     * @return
     */
    public static FileContent createUploadFileContentFragment(FileTransmissionCache cache, ByteBuffer buffer, long size, long position) {
        return FileContent.newBuilder()
                .setBigFileHead(BigFileHead.newBuilder()
                        .setClientUrl(cache.getClientPath())
                        .setServerUrl(cache.getServerPath())
                        .build())
                .setBigFileFragment(BigFileFragment.newBuilder()
                        .setData(ByteString.copyFrom(buffer))
                        .setLength(size)
                        .setPosition(position)
                        .build())
                .setPackageKind(TransmissionConstant.FILE_FRAGMENT)
                .build();
    }

    /**
     * 获取文件传输结果
     *
     * @return
     */
    public static FileContent createUploadFileContentFoot(FileTransmissionCache cache) {
        return FileContent.newBuilder()
                .setBigFileHead(BigFileHead.newBuilder()
                        .setClientUrl(cache.getClientPath())
                        .setServerUrl(cache.getServerPath())
                        .build())
                .setBigFileFoot(BigFileFoot.newBuilder().setIsSuccess(true).build())
                .setPackageKind(TransmissionConstant.FILE_FOOT)
                .build();
    }
}
